package com.ece.demo.controller.commands.sprite;

import org.apache.commons.lang3.StringUtils;

import dev.ece.core.controller.Command;
import dev.ece.suit.heightmap.HeightMapModel;
import dev.ece.suit.loader.Sprite3D;
import dev.ece.util.lib.Matrix4f;
import dev.ece.util.lib.Vector3f;
import dev.ece.util.lib.Vector4f;
import dev.ece.util.tools.Camera;

/**
 * @author 潜行的青衣
 * @see
 */
public abstract class SpriteMoveCommand extends Command {
	
	private Sprite3D<Float> sprite;
	
	private HeightMapModel heightModel;
	
	private Vector3f position;
	
	private Vector4f rotation;
	
	private Camera camera;
	
	private float speed;
	
	public SpriteMoveCommand(
			HeightMapModel heightModel, Sprite3D<Float> sprite, Camera camera,
			Vector3f position, Vector4f rotation, float speed) {
		this.heightModel = heightModel;
		this.sprite = sprite;
		this.camera = camera;
		this.position = position;
		this.rotation = rotation;
		this.speed = speed;
	}
	
	/* (non-Javadoc)
	 * @see com.ece.demo.controller.Command#onExecute()
	 */
	@Override
	public void onExecute() {
		if(!StringUtils.startsWith(sprite.getActionName(), "move")) {
			sprite.action("move-1");
		}
		position.add(this.getDirVector());
		position.setY(this.heightModel.getHeight(position.getX(), position.getZ()));
		//调整摄像机
		this.camera.setPosition(position.getX(), position.getY() + 1, position.getZ());
		
		Matrix4f matrix = new Matrix4f();
		matrix.identity();
		matrix.rotate(-1f, 0, 0, 45);
		
		this.camera.center.splus(this.camera.position, this.camera.center);
		matrix.transform(this.camera.center);
		this.camera.center.add(this.camera.position);
		matrix.transform(this.camera.head);
		
		float distance = 2f;
		//前进方向相反的向量
		Vector3f vector = new Vector3f();
		vector.splus(this.camera.center, this.camera.position);
		vector.normalize();
		vector.setLength(distance);
		this.camera.position.add(vector);
		this.camera.center.add(vector);
	}
	
	@Override
	public void onKeyPress() {
		
	}

	@Override
	public void onKeyRelease() {
		sprite.action("stand-1");
	}
	
	public abstract Vector3f getDirVector();

	public float getSpeed() {
		return speed;
	}

	public void setSpeed(float speed) {
		this.speed = speed;
	}

	public Sprite3D<Float> getSprite() {
		return sprite;
	}

	public HeightMapModel getHeightModel() {
		return heightModel;
	}

	public Vector3f getPosition() {
		return position;
	}

	public Vector4f getRotation() {
		return rotation;
	}

	public Camera getCamera() {
		return camera;
	}

}
